package com.basswold.nasaesa.client;
import java.util.ArrayList;

import com.basswold.nasaesa.client.ConstructionModule.CardinalDirection;
import com.basswold.nasaesa.client.ConstructionModule.ModuleType;

public class SixWing extends BaseConstructor {
	
	FoodWingFactory foodWing1;
	FoodWingFactory foodWing2;
	DormWingFactory dormWing1;
	DormWingFactory dormWing2;
	ControlWingFactory controlWing1;
	ControlWingFactory controlWing2;

	public SixWing(final ModuleList modList) {
		super(modList);
	}

	@Override
	public Module[][] BuildBase() {
		
		/*
		 * Construct a graph of the base
		 */
		ConstructionConnectorModule west1 = 
			(ConstructionConnectorModule)modules.getModuleOfType(ModuleType.PLAIN);
		ConstructionConnectorModule west2 = 
			(ConstructionConnectorModule)modules.getModuleOfType(ModuleType.PLAIN);
		ConstructionConnectorModule west3 = 
			(ConstructionConnectorModule)modules.getModuleOfType(ModuleType.PLAIN);
		ConstructionConnectorModule east1 = 
			(ConstructionConnectorModule)modules.getModuleOfType(ModuleType.PLAIN);
		ConstructionConnectorModule east2 = 
			(ConstructionConnectorModule)modules.getModuleOfType(ModuleType.PLAIN);
		ConstructionConnectorModule east3 = 
			(ConstructionConnectorModule)modules.getModuleOfType(ModuleType.PLAIN);
		ConstructionConnectorModule centralConnector = 
			(ConstructionConnectorModule)modules.getModuleOfType(ModuleType.PLAIN);
		
		west1.setEastConnection(west2);
		west2.setWestConnection(west1);
		west2.setEastConnection(west3);
		west3.setWestConnection(west2);
		centralConnector.setWestConnection(west3);
		west3.setEastConnection(centralConnector);
		centralConnector.setEastConnection(east3);
		east3.setWestConnection(centralConnector);
		east3.setEastConnection(east2);
		east2.setWestConnection(centralConnector);
		east2.setEastConnection(east1);
		east1.setWestConnection(east2);
		
		// Construct the needed building wings.
		this.buildWings();
		
		// Attach completed wings to base structure.
		west1.setWestConnection(controlWing2.finishConstruction());
		east1.setEastConnection(controlWing1.finishConstruction());
		west2.setNorthConnection(dormWing1.finishConstruction());
		west2.setSouthConnection(foodWing2.finishConstruction());
		east2.setNorthConnection(foodWing1.finishConstruction());
		east2.setSouthConnection(dormWing2.finishConstruction());
		
		// Convert graph of base to 2D array
		return convertToOutputModules(trimArray(convertToArray(centralConnector)));
	}

	@Override
	public ArrayList<BuildingWingFactory> getWingFactories() {
		// This will store the separate wings as they are constructed.
		ArrayList<BuildingWingFactory> wings = 
			new ArrayList<BuildingWingFactory>();
		
		// Create wing factories that will construct separate wings
		foodWing1 = new FoodWingFactory(modules);
		foodWing2 = new FoodWingFactory(modules);
		dormWing1 = new DormWingFactory(modules);
		dormWing2 = new DormWingFactory(modules);
		controlWing1 = new ControlWingFactory(modules);
		controlWing2 = new ControlWingFactory(modules);
		
		controlWing1.initialize(CardinalDirection.WEST, true);
		controlWing2.initialize(CardinalDirection.EAST, true);
		foodWing1.initialize(CardinalDirection.SOUTH, true);
		foodWing2.initialize(CardinalDirection.NORTH, true);
		dormWing1.initialize(CardinalDirection.SOUTH, false);
		dormWing2.initialize(CardinalDirection.NORTH, false);
		
		wings.add(controlWing1);
		wings.add(foodWing1);
		wings.add(dormWing1);
		wings.add(controlWing2);
		wings.add(foodWing2);
		wings.add(dormWing2);
		
		return wings;
	}
}

